﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GeneticAlgorithm
{
    public class Configuration : IConfiguration
    {
        private Dictionary<string, string> _parameters;

        public Configuration(string name)
        {
            _parameters = new Dictionary<string, string>();
            Name = name;
        }

        public string Name { get; private set; }

        public void Add(string name, string value)
        {
            if (!_parameters.ContainsKey(name))
                _parameters.Add(name,value);
        }

        public void Change(string name, string value)
        {
            _parameters[name] = value;
        }

        public string GetString(string name)
        {
            return _parameters[name];
        }

        public void FromString(string configuration)
        {
            _parameters.Clear();
            var split = configuration.Split(new[] {'|'}, StringSplitOptions.RemoveEmptyEntries);
            foreach (var s in split)
            {
                var keyVal = s.Split(new[] {'='});
                _parameters.Add(keyVal[0], keyVal[1]);
            }
        }

        public void FromConfiguration(Configuration config)
        {
            _parameters = new Dictionary<string, string>(config._parameters);
        }

        public override string ToString()
        {
            var result = string.Empty;

            foreach (var parameter in _parameters)
            {
                result += parameter.Key + "=" + parameter.Value + "|";
            }

            return result;
        }
    }
}
